package com.example.shop_2208.adapter

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.shop_2208.R
import com.example.shop_2208.entity.CategoryEntity
import com.example.shop_2208.utils.GlideUtils

class CategoryAdapter:BaseQuickAdapter<CategoryEntity,BaseViewHolder>(R.layout.item_cate) {
    private var seletedId = 14//默认打开的一级分类是电脑办公 （1） 下边的二级分类是Apple (14)
    override fun convert(holder: BaseViewHolder, item: CategoryEntity) {
        holder.setText(R.id.cate_tv,item.category_name)
        item.category_icon?.let {
            GlideUtils.loadCircleImage(context,holder.getView(R.id.cate_iv), it)
        }
        /*val imageView = holder.getView<ImageView>(R.id.cate_iv)*/
        //给整个item添加背景色
        holder.getView<LinearLayout>(R.id.ll).background = if(seletedId==item.id){ColorDrawable(Color.GREEN)}else null

        holder.getView<TextView>(R.id.cate_tv).setTextColor(if(seletedId==item.id){Color.RED}else {Color.GRAY})
    }

    fun setId(id:Int){
        seletedId = id
        notifyDataSetChanged()
    }

}